package com.example.frameboot.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.ChainQuery;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.example.frameboot.been.Employee;
import com.example.frameboot.been.Student;
import com.example.frameboot.been.StudentDto;
import com.example.frameboot.been.StudentReq;
import com.example.frameboot.common.BusinessException;
import com.example.frameboot.common.SpringBeanUtil;
import com.example.frameboot.mapper.TestMapper;
import com.example.frameboot.mapper.EmployeeMapper;
import com.example.frameboot.service.TestService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private TestMapper testMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public List<Student> getList() {
        List<Student> studentDto= testMapper.selectList();
        return studentDto;
    }

    @Override
    public void insertStu(StudentDto dto) {
        Student student = new Student();
        student.setName(dto.getName());
        student.setSex(dto.getSex());
        student.setId(UUID.randomUUID().toString());
        Employee employee = new Employee();
        employee.setEmpId("12345");
        employee.setEmpType(6);

        /**
         * 事务状态
         */
        TransactionStatus transactionStatus = null;
        DataSourceTransactionManager dataSourceTransactionManager = null;
        TransactionDefinition transactionDefinition = null;
        try {
            dataSourceTransactionManager = SpringBeanUtil.getBean(DataSourceTransactionManager.class);
            transactionDefinition = SpringBeanUtil.getBean(TransactionDefinition.class);
            //开启事务
            transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
            employeeMapper.insertTest(employee);
            testMapper.insertStu(student);
            //提交事务
            dataSourceTransactionManager.commit(transactionStatus);
        } catch (Exception e) {
            //手动回滚事务
            if (dataSourceTransactionManager != null) {
                dataSourceTransactionManager.rollback(transactionStatus);
            }
            throw e;
        } finally {
            System.out.println("学生新增");
        }

    }

    @Override
    public void deleteStu(List<String> id) {
        for (String i : id) {
            testMapper.deleteById(i);
        }
    }

    @Override
    public PageInfo<Student> getStudent(StudentReq req) {
        QueryWrapper<StudentReq> wrapper = new QueryWrapper<>();

        PageInfo<Student> list = new PageInfo<>();
        PageMethod.startPage(req.getPageIndex(), req.getPageSize());
        List<Student> students = testMapper.selectStuList(req);
        list.setList(students);
        return(PageInfo<Student>) (PageInfo) list;
    }
    @Override
    public void insertStuList() {
        for (int i = 0; i < 80000; i++) {
            Student student = new Student();
            Random random = new Random();
            student.setId(UUID.randomUUID().toString());
            String sex = String.valueOf(random.nextInt(2));
            student.setSex(sex);
            String name = String.valueOf(random.nextInt(100000));
            student.setName(name);
            testMapper.insertStu(student);
        }
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void insertStuTest(StudentDto dto) {
        Employee employee = new Employee();
        employee.setEmpId("12345");
        employee.setEmpType(1);
        employeeMapper.insertTest(employee);
        Student student = new Student();
        student.setName(dto.getName());
        student.setSex(dto.getSex());
        student.setId(UUID.randomUUID().toString());
        testMapper.insertStu(student);
        //int i = 1/0;
    }

    @Override
    public ChainQuery<Student> lambdaQuery() {
       /* ChainWrapper<List<Student>> wrapper = new ChainWrapper<List<Student>>() {
            @Override
            public BaseMapper<List<Student>> getBaseMapper() {
                return null;
            }

            @Override
            public Wrapper<List<Student>> getWrapper() {
                return null;
            }

        };*/

        //ChainQuery<List<Student>> list =(ChainQuery<List<Student>>) new LambdaQueryChainWrapper<>(testMapper).list();

        /*ChainQuery<List<Student>> listChainQuery =(ChainQuery<List<Student>>) testMapper.selectList(wrapper);*/

        QueryChainWrapper<Student> queryChainWrapper = new QueryChainWrapper<>(testMapper);
        queryChainWrapper.list();
        return queryChainWrapper;
    }


}
